<template>
    <div style="height:100%;width:100%;position:relative;" ref="maindev">
        <div class="pagetop" ref="buttondiv" :style="pagetopstyle">
            <div style="width: 100%">
                <table>
                    <tr>
                        <td align="right">接收日期</td>
                        <td>
                            <el-date-picker v-model="searchParam.datetime1" type="date" placeholder="开始日期"
                                value-format="yyyy-MM-dd" :clearable="false" style="width: 130px"></el-date-picker> 至
                            <el-date-picker v-model="searchParam.datetime2" type="date" placeholder="结束日期"
                                value-format="yyyy-MM-dd" :clearable="false" style="width: 130px"></el-date-picker>
                        </td>
                        <td colspan="4" style="text-align:left">
                            <div class="chargeflag">
                                <el-radio-group v-model="searchParam.charge_flag" size="mini">
                                    <el-radio label="">所有</el-radio>
                                    <el-radio label="1">已计价</el-radio>
                                    <el-radio label="0">未计价</el-radio>
                                    <el-radio label="9">已退费</el-radio>
                                </el-radio-group>
                            </div>
                        </td>
                        <td align="right">计价相关信息</td>
                        <td style="text-align:left">
                            <el-input v-model="searchParam.chargemsg" style="width:120px"></el-input>
                        </td>
                    </tr>
                    <tr v-show="Showflag">
                        <td colspan="2" style="text-align:left">
                            <div style="width:100%">
                                <el-checkbox v-model="searchParam.req_stat1" true-label="1"
                                    false-label="0">显示已作废的申请</el-checkbox>
                                <el-checkbox v-model="searchParam.chargefailcnt" true-label="1"
                                    false-label="0">仅显示计价失败过的记录</el-checkbox>
                            </div>
                        </td>
                        <td align="right">条形码</td>
                        <td style="text-align:left">
                            <el-input v-model="searchParam.barcode" style="width:120px"></el-input>
                        </td>
                        <td style="text-align:right">姓名</td>
                        <td style="text-align:left">
                            <el-input v-model="searchParam.pat_name" style="width:120px"></el-input>
                        </td>
                        <td style="text-align:right">病人类型</td>
                        <td style="text-align:left">
                            <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人类型" :appendtobody="true"
                                v-model="searchParam.pat_typecode" width="120px"></rm-ddl-common>
                        </td>
                        <td style="text-align:right">病人ID</td>
                        <td style="text-align:left">
                            <el-input v-model="searchParam.pat_id" style="width:150px"></el-input>
                        </td>

                    </tr>
                    <tr v-show="Showflag">
                        <td align="right">业务类型</td>
                        <td>
                            <div>
                                <el-radio-group v-model="searchParam.reqtype" size="mini">
                                    <el-radio label="">全部</el-radio>
                                    <el-radio label="C">检验</el-radio>
                                    <el-radio label="H">输血</el-radio>
                                </el-radio-group>
                            </div>
                        </td>
                        <td style="text-align:right">病历号</td>
                        <td style="text-align:left">
                            <el-input v-model="searchParam.pat_no" style="width:120px"></el-input>
                        </td>
                        <td align="right">状态</td>
                        <td>
                            <el-select v-model="searchParam.req_stat" size="mini" clearable style="width:120px">
                                <el-option v-for="item in statList" :key="item.codeno" :label="item.codena"
                                    :value="item.codeno">
                                    <span style="float: left">{{ item.codena }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.codeno }}</span>
                                </el-option>
                            </el-select>
                        </td>
                        <td align="right">执行科室</td>
                        <td>
                            <rm-ddl-common datatype="dept" dataname="科室" v-model="searchParam.perform_dept" size="mini"
                                width="120px"></rm-ddl-common>
                        </td>
                        <td align="right">送检医院</td>
                        <td>
                            <rm-ddl-common datatype="hospital" dataname="医院" v-model="searchParam.hospcode"
                                width="150px"></rm-ddl-common>
                        </td>
                        <!-- <td align="right">
                            <el-button icon="el-icon-search" type="primary" @click="FindData" v-has="'PINQUERY'">查询</el-button>
                            <el-button icon="el-icon-delete" @click="ClearFailNum">清除失败次数</el-button>
                        </td> -->
                    </tr>
                </table>
                <div style="width:100%;text-align:right">
                    <el-link :underline="false" @click="OpenMoreSel">{{ Showflag ? "收起▲" : "展开▼" }}</el-link>
                    <el-button icon="el-icon-search" type="primary" @click="FindData" v-has="'PINQUERY'">查询</el-button>
                    <el-button icon="el-icon-delete" @click="ClearFailNum">清除失败次数</el-button>
                </div>
            </div>

        </div>
        <div class="pagecenter" :style="pagecenter">
            <div class="middle-center" ref="xTableDiv">
                <vxe-table ref="xTable" :data="tableData" :loading="loading" :keyboard-config="keyConfig"
                    :keyboard-local-config="keyLocalConfig" :select-config="selectConfig" :sort-config="{ trigger: 'cell' }"
                    :edit-config="{ trigger: 'click', mode: 'cell' }" border fit :height="reportheight" header-align="center"
                    resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe
                    :row-class-name="rowClassName" :cell-class-name="cellClassName" @current-change="currentChangeEvent"
                    highlight-current-row show-footer :footer-method="footerMethod"
                    :footer-cell-class-name="footerCellClassName" @cell-dblclick="xTableDblEvent">
                    <vxe-table-column field="pat_typename" sortable title="病人来源" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="create_dt" sortable title="创建时间" width="140" show-overflow></vxe-table-column>
                    <vxe-table-column field="cp_selection" title="选择" width="40px" align="center" sortable>
                        <template v-slot:header="{ column }">
                            <el-checkbox v-model="selectall" @change="ChkAll($event)"></el-checkbox>
                        </template>
                        <template v-slot="{ row }">
                            <el-checkbox v-model="row.cp_selection" @change="ChkEven(row)" true-label="1"
                                false-label="0"></el-checkbox>
                        </template>
                    </vxe-table-column>
                    <vxe-table-column field="pat_no" sortable title="病历号" width="100" show-overflow></vxe-table-column>
                    <vxe-table-column field="pat_name" sortable title="姓名" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="req_groupna" sortable title="分单类别" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="barcode" sortable title="条形码" width="120" show-overflow></vxe-table-column>
                    <vxe-table-column field="emer_flag" sortable title="加急" width="50" :formatter="format"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="sample_items" sortable title="申请项目(简称，部分)" width="150"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="req_statname" sortable title="状态" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="base_amount" sortable title="应收金额" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="charge_flag" sortable title="计价标志" width="80" :formatter="format"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="chargefailmsg" sortable title="计价相关信息或失败相关信息" width="200"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="chargefailcnt" sortable title="计价失败次数" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="charge_username" sortable title="计价人" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="charge_dt" sortable title="计价时间" width="140" show-overflow></vxe-table-column>
                    <vxe-table-column field="recieve_dt" sortable title="签收时间" width="140"></vxe-table-column>
                    <vxe-table-column field="recieve_username" sortable title="签收人" width="80"></vxe-table-column>
                    <vxe-table-column field="recieve_host" sortable title="签收电脑" width="100"></vxe-table-column>
                    <vxe-table-column field="amount" sortable title="实收金额" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="req_deptname" sortable title="申请科室" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="req_docname" sortable title="申请医生" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="req_bedno" sortable title="床号" width="60" show-overflow></vxe-table-column>
                    <vxe-table-column field="specimen_name" sortable title="标本类型" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="pat_sexname" sortable title="性别" width="50" show-overflow></vxe-table-column>
                    <vxe-table-column field="labadd" sortable title="实验室增加标志" align="center" width="80" show-overflow>
                        <template v-slot="{ row }">
                            <el-checkbox v-model="row.labadd" disabled></el-checkbox>
                        </template>
                    </vxe-table-column>
                    <vxe-table-column field="create_username" sortable title="创建人" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="charge_typename" sortable title="费别代码" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="req_dt" sortable title="申请时间" width="140" show-overflow></vxe-table-column>
                    <!-- <vxe-table-column field="create_dt" sortable title="创建时间" width="140" show-overflow></vxe-table-column> -->
                    <vxe-table-column field="pat_id" sortable title="病人ID" width="120" show-overflow></vxe-table-column>
                    <vxe-table-column field="inp_date" sortable title="入院日期" width="140" show-overflow></vxe-table-column>
                    <vxe-table-column field="input_dt" sortable title="上机时间" width="140" show-overflow></vxe-table-column>
                    <!-- <vxe-table-column field="test_username" sortable title="上机人" width="80" show-overflow></vxe-table-column> -->
                    <vxe-table-column field="report_dt" sortable title="报告时间" width="140" show-overflow></vxe-table-column>
                    <vxe-table-column field="report_username" sortable title="检验医生姓名" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="rechk_username" sortable title="核对医生姓名" width="80"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="saleman" sortable title="销售员" width="80" show-overflow></vxe-table-column>
                    <vxe-table-column field="pat_cardno" sortable title="就诊卡号" width="100" show-overflow></vxe-table-column>
                    <vxe-table-column field="inp_id" sortable title="住院标识/就诊号" width="100" show-overflow></vxe-table-column>
                    <vxe-table-column field="perform_deptname" sortable title="执行科室" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="req_wardname" sortable title="病区代码" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="original_reqno" sortable title="原始申请单号" width="100"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="sample_detail" sortable title="标准备注说明" width="150"
                        show-overflow></vxe-table-column>
                    <vxe-table-column field="req_reason" sortable title="检验目的" width="100" show-overflow></vxe-table-column>
                    <vxe-table-column field="reqtype" sortable title="申请单类别" width="100" :formatter="format"
                        show-overflow></vxe-table-column>
                </vxe-table>
            </div>
        </div>
    </div>
</template>

<script>
import XEUtils from "xe-utils"; //引入工具类
export default {
    name: "LIS431",
    data() {
        return {
            keyConfig: {
                isDel: true,
                isTab: true,
                isEdit: true
            },
            keyLocalConfig: {
                isArrow: true
            },
            selectConfig: {
                tigger: "row"
            },
            searchParam: {
                datetime1: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                datetime2: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                pat_typecode: "3",
                charge_flag: "0",
                chargemsg: "",
                hospcode: "",
            },
            Showflag: false, //展示更多标志
            statList: [],
            tableData: [],
            loading: false,
            selrow: {},
            selectall: false, //全选
            //展开样式
            pagetopstyle: {
                position: "absolute",
                height: "65px",
                width: "100%"
            },
            pagecenter: {
                position: "absolute",
                top: "70px",
                bottom: "0px",
                overflow: "auto",
                width: "100%",
                left: "0px"
            },
            reportheight: "auto"
        };
    },
    mounted() {
        this.queryreqstat();
    },
    methods: {
        //全选/取消
        ChkAll(e) {
            if (this.tableData != null && this.tableData.length > 0) {
                this.tableData.forEach(item => {
                    item.cp_selection = this.selectall == true ? "1" : "0";
                });
            }
        },
        //点击勾选
        ChkEven(row) {
            row.cp_selection == "0" ? "1" : "0";
        },
        //点击勾选
        OpenMoreSel() {
            let mainheight = this.$refs.maindev.offsetHeight;
            this.Showflag = !this.Showflag;
            if (this.Showflag) {
                this.pagetopstyle.height = "130px";
                this.pagecenter.top = "135px";
                this.reportheight = mainheight - 135;
                // if (this.is_ym) {
                //     this.ymresultheight = mainheight - 373;
                // } else {
                //     this.resultheight = mainheight - 175;
                // }
            } else {
                this.pagetopstyle.height = "65px";
                this.pagecenter.top = "70px";
                this.reportheight = mainheight - 70;
                // if (this.is_ym) {
                //     this.ymresultheight = mainheight - 343;
                // } else {
                //     this.resultheight = mainheight - 110;
                // }
            }
        },
        queryreqstat() {
            this.selectall = false;
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS431",
                    method: "post",
                    data: {
                        action: "reqstatlist"
                    }
                })
                .then(response => {
                    if (response.errcode == 1) {
                        this.statList = response.Data;
                    }
                    console.log(response.secHospCode);
                    if (response.secHospCode) {
                        console.log(response.secHospCode);
                        this.searchParam.hospcode = response.secHospCode;
                    }
                })
                .catch(e => { });
        },
        FindData() {
            console.log(this.searchParam);
            this.loading = true;
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS431",
                    method: "post",
                    data: {
                        action: "querydata",
                        ...this.searchParam
                    }
                })
                .then(response => {
                    if (response.errcode == 1) {
                        this.tableData = response.Data;
                        this.loading = false;
                        if (response.Data.length == 0) {
                            return this.$message({
                                message: "未查询到符合条件的数据",
                                type: "warning"
                            });
                        }
                    } else {
                        this.loading = false;
                        return this.$message({
                            message: "申请单计价查询异常",
                            type: "warning"
                        });
                    }
                })
                .catch(e => {
                    this.loading = false;
                });
        },
        ClearFailNum() {
            //添加勾选/多选清除
            var that = this;
            let clearData = [];
            var barstr = "";
            clearData = this.tableData.filter(item => {
                return item.cp_selection == "1";
            });
            if (clearData && clearData.length == 0) {
                that.$message({
                    message: "请选择清除失败次数的对应数据。",
                    type: "warning"
                });
                return false;
            } else {
                clearData.forEach(e => {
                    barstr += "'" + e.barcode + "',";
                });
                barstr = barstr.substring(0, barstr.lastIndexOf(","));
            }
            // if (that.selrow.barcode == null || that.selrow.barcode == "") {
            //     that.$message({
            //         message: "请选择清除失败次数的对应数据",
            //         type: "warning",
            //     });
            //     return false;
            // }
            // if (that.selrow.charge_flag != 0 || that.selrow.chargefailcnt < 5) {
            //     return false;
            // }
            that.$confirm("你是否要清除选中的申请单的计价失败次数（仅针对未计价的失败次数超过5次的有效）?", "提示", {
                confirmButtonText: "是",
                cancelButtonText: "否",
                type: "warning"
            }).then(() => {
                that.operatetype = 3;
                that.$tool
                    .request({
                        url: "LIS/LIS4/LIS431",
                        method: "post",
                        data: {
                            action: "clearfailnum",
                            // barcode: that.selrow.barcode,
                            barstr: barstr
                        }
                    })
                    .then(response => {
                        if (response.errcode != "1") {
                            that.$message({
                                message: "清除失败！" + response.Data.Item2,
                                type: "warning"
                            });
                        } else {
                            that.$message({
                                message: "清除成功！",
                                type: "success"
                            });
                            this.FindData();
                            //     // 数据库清空字段，页面删除当前行
                            //     var delrow = this.$refs.xTable.getCurrentRow();
                            //     if (delrow) {
                            //         var alld = this.$refs.xTable.tableData;
                            //         var rowindex = alld.indexOf(delrow);
                            //         this.$refs.xTable.remove(delrow);
                            //         let havedata = this.$refs.xTable.tableData;
                            //         if (havedata.length > 0) {
                            //             if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                            //             let selectrow = havedata[rowindex];
                            //             this.$refs.xTable.setCurrentRow(selectrow);
                            //         }
                            //     }
                        }
                    })
                    .catch(e => { });
            });
            // this.tableData = [];
            this.searchParam = {
                datetime1: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                datetime2: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                charge_flag: "0"
            };
        },
        currentChangeEvent({ row }) {
            this.selrow = row;
        },
        format({ row, column, cellValue }) {
            if (column.property == "charge_flag") {
                if (cellValue == "0") {
                    return "未计价";
                } else if (cellValue == "1") {
                    return "已计价";
                } else if (cellValue == "8") {
                    return "等待退费";
                } else if (cellValue == "9") {
                    return "已退费";
                } else if (cellValue == "5") {
                    return "免费";
                } else {
                    return "";
                }
            } else if (column.property == "reqtype") {
                if (cellValue == "C") {
                    return "检验";
                } else {
                    return "输血";
                }
            } else if (column.property == "emer_flag") {
                if (cellValue == "1") {
                    return "急";
                } else {
                    return "";
                }
            }
        },
        rowClassName({ row, rowIndex }) {
            if (row.req_stat == "1") {
                return "req_stat1";
            } else if (row.req_stat == "2") {
                return "req_stat2";
            } else if (row.req_stat == "3") {
                return "req_stat3";
            } else if (row.req_stat == "4") {
                return "req_stat4";
            } else if (row.req_stat == "5") {
                return "req_stat5";
            } else if (row.req_stat == "6") {
                return "req_stat6";
            } else if (row.req_stat == "8") {
                return "req_stat8";
            } else if (row.req_stat == "9") {
                return "reqstat9";
            } else {
                return "";
            }
        },
        cellClassName({ column, row }) {
            if (column.property == "emer_flag") {
                if (row.emer_flag == "1") {
                    return "col-red";
                } else {
                    return "";
                }
            }
        },
        footerCellClassName({ $rowIndex, column, columnIndex }) {
            if (columnIndex === 0) {
                return "col-red";
            }
        },
        footerMethod({ columns, data }) {
            return [
                columns.map((column, columnIndex) => {
                    if (columnIndex === 0) {
                        return "";
                    }
                    if (["base_amount", "amount"].includes(column.property)) {
                        return XEUtils.sum(data, column.property);
                    }
                    return null;
                })
            ];
        },
        xTableDblEvent({ row, rowIndex, $rowIndex, column }) {
            if (column.property == "chargefailmsg" && row.chargefailmsg && row.chargefailmsg != null) { // 弹窗显示字段信息
                this.$alert(row.chargefailmsg, '计价相关信息或失败相关信息', {
                    confirmButtonText: '确定',
                    callback: action => { },
                });
            }
        },
    }
};
</script>

<style scoped>
::v-deep .vxe-input--wrapper .vxe-input {
    height: auto;
    width: 200px;
    border-radius: 0px;
}

::v-deep .el-button--default.btn-custom-cancel {
    float: right !important;
    margin-left: 10px;
}

.pagetop {
    left: 0;
    right: 0;
    height: 215px;
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    border: solid 1px #ccc;
    position: absolute;
    z-index: 99;
    overflow-x: auto;
    white-space: nowrap;
}

.chargeflag {
    padding: 0px 10px;
}

.pagecenter {
    position: absolute;
    top: 134px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}

.middle-center {
    position: absolute;
    height: 100%;
    right: 0;
    left: 0;
    height: 100%;
}

table tr td:nth-child(odd) {
    text-align: right;
    white-space: nowrap;
}

table tr td:nth-child(even) {
    text-align: left;
}

table {
    font-size: 12px;
    border-collapse: separate;
    border-spacing: 5px;
}

::v-deep .el-checkbox__inner::after {
    border: 2px solid #000000 !important;
    box-sizing: content-box;
    content: "";
    border-left: 0 !important;
    border-top: 0 !important;
    height: 7px;
    left: 4px;
    position: absolute;
    top: 1px;
    transform: rotate(45deg) scaleY(0);
    width: 3px;
    transition: transform 0.15s ease-in 0.05s;
    transform-origin: center;
}

::v-deep .el-checkbox__input.is-disabled+span.el-checkbox__label {
    color: #000000;
    cursor: not-allowed;
}

::v-deep .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner+.el-input.is-disabled .el-input__inner {
    background-color: #ffffff !important;
}

::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
    width: 130px;
}

::v-deep .chargeflag .el-radio {
    margin-right: 10px !important;
}

::v-deep .chargeflag .el-radio__label {
    width: 35px;
    display: inline-block;
}

::v-deep .vxe-table .vxe-body--column.col-red {
    color: red;
}

::v-deep .vxe-table .vxe-body--row.row--current {
    background-color: #b6e8ff !important;
}

::v-deep .middle-center .vxe-table {
    height: 100%;
}

::v-deep .middle-center .vxe-table--body-wrapper {
    height: calc(100% - 54px) !important;
}</style>